/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rational.sheet.table;

import java.util.ArrayList;
import org.rational.parser.type.DefaultTypes;
import org.rational.parser.type.Type;
import org.rational.parser.type.standart.Textual;
import org.rational.parser.type.wraps.TrimType;
import org.rational.sheet.table.cell.model.CellAddress;

/**
 *
 * @author Overmind
 */
public class LinkType
    extends TrimType<CellAddress>
{
    private RationalTableData tableData;
    
    public LinkType(RationalTableData tableData)
    {
        super("Link");
        this.tableData = tableData;
    }

    @Override
    public CellAddress valueOfTrimString(String string) throws NumberFormatException
    {
        return CellAddress.parseCellAddress(string);
    }
    
    @Override
    public String objectToString(CellAddress address)
    {
        String value = tableData.getValueAt(address);
        
        ArrayList<Type> types = new ArrayList<>();
        types.add(DefaultTypes.DECIMAL);
        types.add(DefaultTypes.NUMERIC);
        types.add(DefaultTypes.LOGICAL);
        
        for (Type type : types)
        {
            try
            {
                return type.objectToString(type.valueOfString(value));
            }
            catch(NumberFormatException nfe)
            {
                continue;
            }
        }
        
        return Textual.QUOTE + value + Textual.QUOTE;
    }
}
